package com.dycx.api.controller.app.food;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.dycx.common.RestResponse;
import com.dycx.common.annotation.Authorization;
import com.dycx.common.annotation.WriterLog;
import com.dycx.common.context.UserInfoContext;
import com.dycx.common.enums.BizTypeEnum;
import com.dycx.food.dto.OrderCreateDTO;
import com.dycx.food.dto.OrderDTO;
import com.dycx.food.dto.OrderGoodsDTO;
import com.dycx.food.dto.ShoppingCartDTO;
import com.dycx.food.service.OrderFoodService;
import com.dycx.food.service.OrderGoodsFoodService;
import com.dycx.food.service.ShoppingCartFoodService;
import com.dycx.food.vo.OrderVO;
import com.dycx.food.vo.ShoppingCartVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/app/food/order")
public class OrderFoodApiController {

    @Resource
    private OrderFoodService orderService;

    @Resource
    private ShoppingCartFoodService shoppingCartFoodService;

    @Resource
    private OrderGoodsFoodService orderGoodsFoodService;

    /**
     * 创建订单
     **/
    @Authorization
    @WriterLog
    @PostMapping("/createOrder")
    public RestResponse<?> createOrder(@RequestBody @Valid OrderCreateDTO orderCreateDto) {
        if (ObjectUtil.isEmpty(orderCreateDto.getGoodsParam())){
            List<ShoppingCartDTO> goodsParam = new ArrayList<>();

            List<ShoppingCartVO> shoppingCartList = shoppingCartFoodService.getList();
            shoppingCartList.forEach(item -> {
                goodsParam.add(BeanUtil.toBean(item, ShoppingCartDTO.class));
            });

            orderCreateDto.setGoodsParam(goodsParam);
        }

        return RestResponse.success(orderService.createOrder(orderCreateDto));
    }

    /**
     * 分页
     **/
    @Authorization
    @PostMapping("/getPage")
    public RestResponse<?> getPage(@RequestBody OrderDTO orderDto) {
        orderDto.setUid(UserInfoContext.get().getUserId());
        orderDto.setUserDel(false);

        return RestResponse.success(orderService.getPage(orderDto));
    }


    /**
     * 根据订单号查询订单信息
     **/
    @Authorization
    @GetMapping("/getOrderDetailByOrderNo")
    public RestResponse<?> getOrderDetailByOrderNo(String orderNo) {
        OrderVO orderVO = orderService.getOrderDetailByOrderNo(orderNo, true, false);
        if (!UserInfoContext.get().getUserId().equals(orderVO.getUid())) {
            RestResponse.fail("无权操作");
        }

        return RestResponse.success(orderVO);
    }

    /**
     * 用户取消订单
     **/
    @Authorization
    @WriterLog
    @GetMapping("/cancelOrder")
    public RestResponse<?> cancelOrder(String orderNo) {
        return orderService.cancelOrder(orderNo) ? RestResponse.success() : RestResponse.fail();
    }

    /**
     * 完成订单
     **/
    @Authorization
    @WriterLog
    @GetMapping("/completeOrder")
    public RestResponse<?> completeOrder(String orderNo) {
        return orderService.completeOrder(orderNo) ? RestResponse.success() : RestResponse.fail();
    }

    /**
     * 已制作
     **/
    @Authorization
    @WriterLog
    @GetMapping("/orderGoodsStatusEnd")
    public RestResponse<?> orderGoodsStatusEnd(Integer orderGoodsId) {
        return orderService.orderGoodsStatusEnd(orderGoodsId) ? RestResponse.success() : RestResponse.fail();
    }

    /**
     * 已出餐
     **/
    @Authorization
    @WriterLog
    @GetMapping("/orderGoodsStatusComplete")
    public RestResponse<?> orderGoodsStatusComplete(Integer orderGoodsId) {
        return orderService.orderGoodsStatusComplete(orderGoodsId) ? RestResponse.success() : RestResponse.fail();
    }

    /**
     * 修改订单项备注
     **/
    @WriterLog(bizType = BizTypeEnum.MALL_ORDER, bizValue = "{#dto.id}")
    @PostMapping("/updateOrderGoodsRemark")
    public RestResponse<?> updateOrderGoodsRemark(@RequestBody OrderGoodsDTO dto) {
        return orderGoodsFoodService.updateRemark(dto) ? RestResponse.success() : RestResponse.fail();
    }
}
